import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { ProductCategoryServiceProxy, CategoryCreateDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/app-component-base';
import { AppConsts } from '@shared/AppConsts';

@Component({
    selector: 'createCategoryModal',
    templateUrl: 'category-add.component.html',
    styleUrls: ['category-add.component.scss']
})
export class CategoryAddComponent extends AppComponentBase {
    @ViewChild('createCategoryModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    active: boolean = false;
    saving: boolean = false;
    category: CategoryCreateDto = null;

    constructor(
        injector: Injector,
        private _categoryService: ProductCategoryServiceProxy
    ) {
        super(injector);
    }

    show(): void {
        this.active = true;
        this.modal.show();
        this.category = new CategoryCreateDto();
    }

    save(): void {

        this.saving = true;
        this._categoryService.createAsync(this.category)
            .finally(() => { this.saving = false; })
            .subscribe(() => {
                this.notify.info('分类创建成功!');
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}